<?php


require_once(dirname(__FILE__).'/../common/log.php');
require_once(dirname(__FILE__).'/../utils/HttpUtils.php');


define("APP_ID", 'zuprx235');
define("APP_KEY", 'e04d5114a0c56836e1acf734f200d422');


//内置用户id
define("USERID_SYSTEM", 1000); //系统
define("USERID_SERVER", 1001); //客服
define("USERID_OPERAT", 1002); //运营

//审核状态
define("AUDIT_STATUS_WAIT", 0); //待审
define("AUDIT_STATUS_PASS", 1); //通过
define("AUDIT_STATUS_REFUSE", 2); //拒绝
    
//收支类型
define("COST_TYPE_BEAN", 1); //心豆
define("COST_TYPE_COIN", 2); //金币
define("COST_TYPE_SCORE", 3); //积分

//账号状态
define("ACCOUNT_STATUS_BANNED", 0); //禁用
define("ACCOUNT_STATUS_NORMAL", 1); //正常
define("ACCOUNT_STATUS_DELETED", 2); //注销

//账号类型
define("ACCOUNT_TYPE_USER", 1); //用户
define("ACCOUNT_TYPE_TEST", 2); //测试
define("ACCOUNT_TYPE_PLAT", 3); //官方
define("ACCOUNT_TYPE_KEFU", 4); //客服
    
//数据状态
define("DATA_STATUS_DISABLE", 0); //禁用
define("DATA_STATUS_ENABLE", 1); //启用
define("DATA_STATUS_DELETE", 2); //删除
define("DATA_STATUS_HIDDEN", 3); //隐藏

//通话状态
define("CALL_STATUS_WAITING", 0); //等待接听
define("CALL_STATUS_STARTED", 1); //开始通话
define("CALL_STATUS_STOPPED", 2); //通话结束

//通话结束类型
define("CALL_ENDTYPE_CALLER", 1); //主叫挂断
define("CALL_ENDTYPE_CALLEE", 2); //被叫挂断
define("CALL_ENDTYPE_TIMEOUT", 3); //接听超时
define("CALL_ENDTYPE_BUSY", 4); //被叫忙
define("CALL_ENDTYPE_SYSTEM", 5); //系统挂断

//消息类型
define("MSG_TYPE_TEXT", 200); //文字
define("MSG_TYPE_IMAGE", 201); //图片
define("MSG_TYPE_VOICE", 202); //语音
define("MSG_TYPE_LINK", 203); //链接
define("MSG_TYPE_HINT", 204); //提示

define("MSG_TYPE_CALLOUT", 300); //发起呼叫
define("MSG_TYPE_RINGING", 301); //被叫振铃
define("MSG_TYPE_ANSWER", 302); //被叫接听
define("MSG_TYPE_CONFIRM", 303); //主叫确认
define("MSG_TYPE_HANGUP", 304); //通话挂断


function xor_encode($data, $len)
{
    $key  = APP_KEY;

    for ($i = 0; $i < $len; $i ++)
    {
        $data[$i]  = $data[$i] ^ $key[$i % 32];
    }

    return $data;
}

function xor_decode($data, $len)
{
    $key  = APP_KEY;

    for ($i = 0; $i < $len; $i ++)
    {
        $data[$i]  = $data[$i] ^ $key[$i % 32];
    }

    return $data;
}

function send_kmsg_message($from, $to, $type, $receipt, $msg, $offline, $umid = '', $ts = '')
{
    $data['type']     = (int)$type;
    $data['receipt']  = (int)$receipt;
    $data['msg']      = $msg;

    $body['from']     = (int)$from;
    $body['to']       = json_encode($to);
    $body['ts']       = (int)(empty($ts) ? time() : $ts);
    $body['umid']     = empty($umid) ? date('YmdHis').mt_rand(100, 999) : $umid;
    $body['offline']  = (int)$offline;
    $body['data']     = json_encode($data);
    $body['nonce']    = (int)time();

    //排序
    ksort($body);
    //
    $arr  = [];
    foreach ($body as $k => $v)
    {
        $arr[]  = "$k=$v";
    }
    $src  = join("&",$arr);
    $enc  = md5($src.APP_KEY);

    $head['version']  = 1;
    $head['appId']    = APP_ID;
    $head['sign']     = $enc;

    $ret  = HttpUtils::send_post_request('https://kmsg.cdks.net/api/sendMessage', $head, $body);

    return $ret;
}

function send_offline_push($userId, $title, $content)
{
    $ret  = HttpUtils::send_post_request("http://localhost/offlinePush/sendMessage.php", [], ["userId" => $userId, "title" => $title, "content" => $content]);

    return $ret;
}

